// src/store/modules/interface.store.ts
import { defineStore } from 'pinia'
import { getClinicList as apiGetClinicList } from '@/api/clinic.api'
import { getDepartmentList as apiGetDepartmentList } from '@/api/department.api'
import { getDiseaseList as apiGetDiseaseList } from '@/api/disease.api'
import { getIdentityList as apiGetIdentityList } from '@/api/identity.api'
import { getHospitalLevelList as apiGetHospitalLevelList } from '@/api/hospitalLevel.api'
import { getOrderStatusList as apiGetOrderStatusList } from '@/api/orderStatus.api'
import { getRoleList as apiGetRoleList } from '@/api/role.api'
import { getServerStatusList as apiGetServerStatusList } from '@/api/serverStatus.api'

type IdName = { id: number | string; name: string }

export const useInterfaceStore = defineStore({
  id: 'interfaceStore',

  state: () => ({
    mapInstance: undefined as unknown as any,
    clinicList: [] as IdName[],
    departmentList: [] as IdName[],
    diseaseList: [] as IdName[],
    identityList: [] as IdName[],
    hospitalLevelList: [] as IdName[],
    orderStatusList: [] as IdName[],
    roleList: [] as IdName[],
    serverStatusList: [] as IdName[],
    loading: false,
  }),

  getters: {
    clinicCount: (s) => s.clinicList.length,
  },

  actions: {
    setMapInstance(instance: any) {
      this.mapInstance = instance
    },

    async getClinicList(params: Record<string, any> = {}) {
      this.loading = true
      try {
        const result = await apiGetClinicList(params)
        this.clinicList = Array.isArray(result) ? result : []
      } catch (e) {
        console.error('getClinicList failed:', e)
        this.clinicList = []
      } finally {
        this.loading = false
      }
    },

    async getDepartmentList(params: Record<string, any> = {}) {
      const result = await apiGetDepartmentList(params)
      this.departmentList = Array.isArray(result) ? result : []
    },
    async getDiseaseList(params: Record<string, any> = {}) {
      const result = await apiGetDiseaseList(params)
      this.diseaseList = Array.isArray(result) ? result : []
    },
    async getIdentityList(params: Record<string, any> = {}) {
      const result = await apiGetIdentityList(params)
      this.identityList = Array.isArray(result) ? result : []
    },
    async getHospitalLevelList(params: Record<string, any> = {}) {
      const result = await apiGetHospitalLevelList(params)
      this.hospitalLevelList = Array.isArray(result) ? result : []
    },
    async getOrderStatusList(params: Record<string, any> = {}) {
      const result = await apiGetOrderStatusList(params)
      this.orderStatusList = Array.isArray(result) ? result : []
    },
    async getRoleList(params: Record<string, any> = {}) {
      const result = await apiGetRoleList(params)
      this.roleList = Array.isArray(result) ? result : []
    },
    async getServerStatusList(params: Record<string, any> = {}) {
      const result = await apiGetServerStatusList(params)
      this.serverStatusList = Array.isArray(result) ? result : []
    },
  },

  // 你使用的是 pinia-plugin-persist（不是 persistedstate）
  // 这个插件默认读取 store 里的 persist 配置
  persist: {
    enabled: true,
    strategies: [
      { storage: localStorage, paths: ['clinicList', 'departmentList', 'diseaseList', 'identityList', 'hospitalLevelList', 'orderStatusList', 'roleList', 'serverStatusList'] }
    ]
  },
})
